package Arrays;

import java.util.ArrayList;
import java.util.List;

public class _350_IntersectionofTwoArraysII {
    //较慢
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> num1List = new ArrayList<Integer>();
        List<Integer> num2List = new ArrayList<Integer>();
        List<Integer> resultList = new ArrayList<Integer>();
        int length1 = nums1.length;
        int length2 =nums2.length;
        for(int i=0;i<length1;i++){
            num1List.add(nums1[i]);
        }
        for(int i=0;i<length2;i++){
            num2List.add(nums2[i]);
        }
        for(int i=0;i<length1;i++){
            int val = num1List.get(i);
            for(int j=0;j<num2List.size();j++){
                int tempVal = num2List.get(j);
                if(val==tempVal){
                    resultList.add(tempVal);
                    num2List.remove(j);
                    break;
                }
            }
        }
//        System.out.println(resultList.toString());
        int[] resultNum = new int[resultList.size()];
        int pos = 0;
        for(int i=0;i<resultList.size();i++){
            resultNum[pos++] = resultList.get(i);
        }
        return resultNum;
    }
    /**
     * 加速方法：先使用Arrays.sort()对nums1,nums2进行重排
     */

}
